<template>
    <el-form :model="ruleForm" :rules="rules" ref="rules" label-width="100px" class="demo-ruleForm">
        <el-form-item label="老师名称" prop="name">
            <el-input placeholder="请输入老师名称" v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="老师头像" prop="name">
            <el-upload class="upload-demo" drag action="https://jsonplaceholder.typicode.com/posts/" multiple>
                <i class="el-icon-upload"></i>
                <div class="el-upload__text">点击上传老师的图片</div>
            </el-upload>
        </el-form-item>
        <el-form-item label="老师简介" prop="content">
            <el-input type="textarea" placeholder="请输入简介，控制在300字内" v-model="ruleForm.content"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="sureUpdateTeacher">保存</el-button>
        </el-form-item>
    </el-form>
</template>

<script>
    import { ApiEditTeacherList, ApiUpdateTeacherList } from '../../common/js/api'
    export default {
        data() {
            return {
                loadSureBtn: false,
                id:'', // 编辑当前id
                inputVisible: false,
                inputValue: '',
                ruleForm: {
                    name: '',
                    content: ''
                },
                rules: {
                    name: [
                        { required: true, message: '请输入活动名称', trigger: 'blur' },
                        // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                    ],
                    content: [
                        { required: true, message: '请输入老师简介', trigger: 'blur' }
                    ],
                }
            };
        },
        methods: {
            // 老师确定修改
            sureUpdateTeacher: function () {
                this.$refs['rules'].validate((valid) => {
                    if (valid) {
                        if(this.loadSureBtn){
                            return false
                        }
                        this.loadSureBtn = true;
                        let params = { // 老师编辑
                            id:this.id,
                            name: this.ruleForm.name,
                            image_url:'',
                            content: this.ruleForm.content
                        }
                        ApiUpdateTeacherList(params).then(res => {
                            if(res.data.data === true){
                                this.$message({
                                    message: res.data.msg,
                                    type: 'success'
                                });
                            }
                        }).catch(() => {

                        });
                    }
                })
            }

        },
        created(){
            // 获取编辑对应id
            this.id = this.$router.currentRoute.query.id;
            ApiEditTeacherList({id: this.id}).then(res => {
                if(res.status === 200){
                    this.ruleForm.name = res.data.data[0].name;
                    this.ruleForm.content = res.data.data[0].content
                }
            }).catch(() => {

            });
        }
    }
</script>
<style lang="scss">
    .demo-ruleForm{
        width:600px;
        max-width: 100%;
    }
</style>
